/*
 * Copyright (C) 2010 TopCoder Inc., All Rights Reserved.
 */
package com.hercules.android.receiver.painter;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.Paint.FontMetrics;

import com.hercules.android.receiver.Receiver;
import com.hercules.android.receiver.ReceiverUtil;
import com.hercules.android.receiver.entities.XREFont;
import com.hercules.android.receiver.entities.XREObject;
import com.hercules.android.receiver.entities.XREText;
import com.hercules.android.receiver.entities.XREView;

/**
 * <p>
 * This class is a painter class which will draw a XREText resource on a canvas.
 * </p>
 * 
 * @author TCSASSEMBER
 * @version 1.0
 */
public class XRETextPainter extends XREPainter {
    /**
     * Constructor with a <code>Receiver</code> arguments.
     * 
     * @param receiver
     *            the <code>Receiver</code> instance.
     * @throws IllegalArgumentException
     *             if receiver if null
     */
    public XRETextPainter(Receiver receiver) {
        super(receiver);
    }

    /**
     * This method will paint the resource on canvas.
     * 
     * @param view
     *            the resource to paint
     * @param canvas
     *            the canvas that the resource painted on
     * @throws IllegalArgumentException
     *             if arguments is null
     * @throws XREPainterException
     *             if any error occurs
     */
    public void paint(XREView view, Canvas canvas) throws XREPainterException {
        // check the arguments
        ReceiverUtil.checkNull("view", view);
        ReceiverUtil.checkNull("canvas", canvas);

        if (view.getResource() == 0) {
            return;
        }
        if (!view.getVisible() || !view.getPainting()) {
            return;
        }

        XREText text = (XREText) receiver.getApplicationData().getElementsXREIdToElement().get(view.getResource());
        if (text == null) {
            return;
        }

        Paint paint = new Paint();
        paint.setTextSize(text.getSize());
        FontMetrics fm = paint.getFontMetrics();
        int mFontHeight = (int) (Math.ceil(fm.descent - fm.top));

        if (text.getFont() > 0) {
            // set text font
            Typeface font;
            XREObject object = receiver.getApplicationData().getElementsXREIdToElement().get(text.getFont());
            if (object == null || !(object instanceof XREFont)) {
                throw new XREPainterException("Can't find the asssociated font.");
            }
            XREFont targetFont = (XREFont) object;
            int style = Typeface.NORMAL;
            if ("BOLD".equals(targetFont.getStyle())) {
                style = Typeface.BOLD;
            } else if ("ITALIC".equals(targetFont.getStyle())) {
                style = Typeface.ITALIC;
            } else if ("BOLDITALIC".equals(targetFont.getStyle())) {
                style = Typeface.BOLD_ITALIC;
            }
            if (!ReceiverUtil.isEmpty(targetFont.getFamily())) {
                font = Typeface.create(targetFont.getFamily(), style);
            } else {
                font = Typeface.create(Typeface.SANS_SERIF, style);
            }
            paint.setTypeface(font);
        }

        paint.setColor(text.getColor());
        canvas.drawText(text.getText(), 0, mFontHeight, paint);
    }
}
